package com.sharedmobility.servlet;

import com.google.gson.Gson;
import com.sharedmobility.dao.TripDAO;
import com.sharedmobility.dao.VehicleDAO;
import com.sharedmobility.model.Trip;
import com.sharedmobility.model.Vehicle;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/api/trips/history")
public class TripHistoryServlet extends HttpServlet {
    private TripDAO tripDAO;
    private VehicleDAO vehicleDAO;
    private Gson gson;

    @Override
    public void init() throws ServletException {
        tripDAO = new TripDAO();
        vehicleDAO = new VehicleDAO();
        gson = new Gson();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        try {
            // 获取当前登录用户ID
            HttpSession session = request.getSession();
            Integer userId = (Integer) session.getAttribute("userId");
            
            if (userId == null) {
                throw new Exception("用户未登录");
            }

            // 获取用户的行程历史
            List<Trip> trips = tripDAO.findByUserId(userId);
            List<Map<String, Object>> tripDataList = new ArrayList<>();

            // 组装每个行程的详细信息
            for (Trip trip : trips) {
                Vehicle vehicle = vehicleDAO.findById(trip.getVehicleId());
                Map<String, Object> tripData = new HashMap<>();
                tripData.put("id", trip.getId());
                tripData.put("vehicleType", vehicle.getType());
                tripData.put("plateNumber", vehicle.getPlateNumber());
                tripData.put("startTime", trip.getStartTime());
                tripData.put("startLocation", trip.getStartLocation());
                tripData.put("endTime", trip.getEndTime());
                tripData.put("endLocation", trip.getEndLocation());
                tripData.put("distance", trip.getDistance());
                tripData.put("cost", trip.getCost());
                tripData.put("status", trip.getStatus());
                tripData.put("paid", trip.getPaid());
                tripData.put("rating", trip.getRating());
                tripData.put("comment", trip.getComment());
                tripDataList.add(tripData);
            }

            // 返回成功响应
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("data", tripDataList);
            
            response.getWriter().write(gson.toJson(result));
        } catch (Exception e) {
            // 返回错误响应
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", e.getMessage());
            
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write(gson.toJson(result));
        }
    }
} 